/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-03-13     DRZZZ       the first version
 */
#define THREAD_PRIORITY         1
#define THREAD_TIMESLICE        5
#define LOG_TAG              "Relay"
#define LOG_LVL              LOG_LVL_INFO
#include "head.h"
#include <ulog.h>
#include "board.h"
#include <rtthread.h>
#include <rtdevice.h>
#include <stdio.h>
#include "relay_management.h"

static int Relay_thread(void)
{
    // 八路继电器使能
    rt_pin_mode(RELAY_PIN_1, PIN_MODE_OUTPUT);
    rt_pin_mode(RELAY_PIN_2, PIN_MODE_OUTPUT);
    rt_pin_mode(RELAY_PIN_3, PIN_MODE_OUTPUT);
    rt_pin_mode(RELAY_PIN_4, PIN_MODE_OUTPUT);
    rt_pin_mode(RELAY_PIN_5, PIN_MODE_OUTPUT);
    rt_pin_mode(RELAY_PIN_6, PIN_MODE_OUTPUT);
    rt_pin_mode(RELAY_PIN_7, PIN_MODE_OUTPUT);
    rt_pin_mode(RELAY_PIN_8, PIN_MODE_OUTPUT);
    // 限位开关使能
    rt_pin_mode(  KEY_PIN_1,  PIN_MODE_INPUT);
    rt_pin_mode(  KEY_PIN_2,  PIN_MODE_INPUT);
#if (CODE == SECOND)
    // 电机驱动使能
    rt_pin_mode(    MOTO_AIN_1, PIN_MODE_OUTPUT);
    rt_pin_mode(    MOTO_AIN_2, PIN_MODE_OUTPUT);
    rt_pin_mode(    MOTO_BIN_1, PIN_MODE_OUTPUT);
    rt_pin_mode(    MOTO_BIN_2, PIN_MODE_OUTPUT);
    rt_pin_mode(  MOTO_AIN_PWM, PIN_MODE_OUTPUT);
    rt_pin_mode(  MOTO_BIN_PWM, PIN_MODE_OUTPUT);
#endif
    // 水位使能使能
//    rt_pin_mode(  KEY_PIN_1,  PIN_MODE_INPUT);
//    rt_pin_mode(  KEY_PIN_2,  PIN_MODE_INPUT);
//    rt_pin_mode(  KEY_PIN_2,  PIN_MODE_INPUT);
    LOG_I("--Relay_management init over!");
    return RT_EOK;
}
INIT_DEVICE_EXPORT(Relay_thread);

void Set_Peripheral(uint8_t Peripheral_name, uint8_t relay_state)
{
    if(Peripheral_name == RELAY_PIN_1)
        rt_pin_write(RELAY_PIN_1, relay_state);
    if(Peripheral_name == RELAY_PIN_2)
        rt_pin_write(RELAY_PIN_2, relay_state);
    if(Peripheral_name == RELAY_PIN_3)
        rt_pin_write(RELAY_PIN_3, relay_state);
    if(Peripheral_name == RELAY_PIN_4)
        rt_pin_write(RELAY_PIN_4, relay_state);
    if(Peripheral_name == RELAY_PIN_5)
        rt_pin_write(RELAY_PIN_5, relay_state);
    if(Peripheral_name == RELAY_PIN_6)
        rt_pin_write(RELAY_PIN_6, relay_state);
    if(Peripheral_name == RELAY_PIN_7)
        rt_pin_write(RELAY_PIN_7, relay_state);
    if(Peripheral_name == RELAY_PIN_8)
        rt_pin_write(RELAY_PIN_8, relay_state);
    if(Peripheral_name == RELAY_PIN_8)
        rt_pin_write(RELAY_PIN_8, relay_state);
#if (CODE == SECOND)
    if(Peripheral_name == RIGHTMOTO) {
        if(relay_state == PUSH) {
            rt_pin_write(RIGHTMOTO_IN1, PIN_HIGH);
            rt_pin_write(RIGHTMOTO_IN2,  PIN_LOW);
        }
        else if(relay_state == PULL) {
            rt_pin_write(RIGHTMOTO_IN1,  PIN_LOW);
            rt_pin_write(RIGHTMOTO_IN2, PIN_HIGH);
        }
        else if(relay_state == STOP) {
            rt_pin_write(RIGHTMOTO_IN1,  PIN_LOW);
            rt_pin_write(RIGHTMOTO_IN2,  PIN_LOW);
        }
    }
    if(Peripheral_name == LEFTMOTO) {
        if(relay_state == PUSH) {
            rt_pin_write(LEFTMOTO_IN1,  PIN_HIGH);
            rt_pin_write(LEFTMOTO_IN2,   PIN_LOW);

        }
        else if(relay_state == PULL) {
            rt_pin_write(LEFTMOTO_IN1,   PIN_LOW);
            rt_pin_write(LEFTMOTO_IN2,  PIN_HIGH);
        }
        else if(relay_state == STOP) {
            rt_pin_write(LEFTMOTO_IN1,   PIN_LOW);
            rt_pin_write(LEFTMOTO_IN2,   PIN_LOW);
        }
    }
#endif
}
